//
//  HEDoctorCommentListViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/17.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEDoctorCommentListViewController.h"
#import "HEDoctorCommentCell.h"
#import "HEDoctorCommentModel.h"

@interface HEDoctorCommentListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEDoctorCommentModel *> * dataSourceArr;

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;

@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) GDFiterCellModel *commentModel;

@end

@implementation HEDoctorCommentListViewController

- (NSMutableArray<HEDoctorCommentModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"评价";
    
    _pageNum = 1;
    _pageSize = 10;
        
    [self setCustomTableView:self.tableView];
    [self showEmpty];
 
    [self initCell];
    
    [self loadData];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
        _tableView.separatorColor = KHexColor(@"EEEEEE");
        _tableView.scrollsToTop = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = KWhiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        
        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

            weakSelf.isFromHeadRefresh = YES;
            weakSelf.pageNum = 1;
            [weakSelf loadData];
        }];
        
        self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            weakSelf.isFromHeadRefresh = NO;
            weakSelf.pageNum += 1;
            [weakSelf loadData];
        }];
        
        [_tableView registerClass:[HEDoctorCommentCell class] forCellReuseIdentifier:@"HEDoctorCommentCell"];
    }
    return _tableView;
}
 
- (void)initCell {
    // 总段
    _sections = [NSMutableArray array];
    NSMutableArray *classArr = [NSMutableArray array];
 

    // 评论
    GDFiterCellModel *commentModel = [GDFiterCellModel new];
    commentModel.cellKlass = HEDoctorCommentCell.class;
    commentModel.cellType = GDFiterCellTypeDoctorComment;
    _commentModel = commentModel;
   
    // 添加的时候注意顺序，暂未对顺序做处理
    classArr = @[commentModel].mutableCopy;
    [self.tableView mc_registerCellByFiterModels:classArr];

    [_sections addObject: commentModel];
    [_tableView reloadData];
}

- (void)loadData {
 
    
    HEPageRequest *params = [HEPageRequest new];
    params.pageNum = _pageNum;
    params.pageSize = _pageSize;
    [params addParmas:_userId key:@"target_id"];
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KDoctorComments parameters:params success:^(id  _Nonnull responseObject) {
        
        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }
        
        HEPageResponse *response = [HEPageResponse mj_objectWithKeyValues:responseObject];
                
        [self.dataSourceArr addObjectsFromArray:[HEDoctorCommentModel mj_objectArrayWithKeyValuesArray:response.data.list]];
        self.formModel.doc_commentList = self.dataSourceArr;
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.list == 0) {
                [Dialog toastBottom:@"没有更多数据了~"];
            }
        }
        
     } failure:^(NSError * _Nonnull error) {
         [self.tableView.mj_header endRefreshing];
         [self.tableView.mj_footer endRefreshing];
     }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:_commentModel.cellKlass];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    _commentModel.fiterCellModelBlock((MCTableBaseCell *)cell, _commentModel, self.formModel, indexPath);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(_commentModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
               [cell refreshContent:self.commentModel formModel:self.formModel indexPath:indexPath];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

#pragma mark -


@end
